package org.example.demos.pattern.strategy_pattern;

import org.example.demos.pattern.strategy_pattern.strategy.LocalLoginStrategy;
import org.example.demos.pattern.strategy_pattern.strategy.LoginStrategy;
import org.example.demos.pattern.strategy_pattern.strategy.WeChatLoginStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author liujiaxiang
 *
 * 静态工厂
 */
public class LoginStrategyFactory {
    private static Map<String, LoginStrategy> strategies = new ConcurrentHashMap<String,LoginStrategy>();
 
    static {
        strategies.put(LoginTypeEnum.LOCAL.getName(), new LocalLoginStrategy());
        strategies.put(LoginTypeEnum.WECHAT.getName(), new WeChatLoginStrategy());
    }

    public static LoginStrategy getStrategy(String loginType) throws Exception {
        LoginTypeEnum loginTypeEnum = LoginTypeEnum.find(loginType);
        if (loginTypeEnum==null){
            throw new Exception("类型不存在");
        }
        return strategies.get(loginType);
    }
}